package com.gy.service.impl;

import com.aaa.entity.OrderBackfee;
import com.aaa.entity.OrderBackfeeItem;
import com.aaa.entity.OrderCharge;
import com.aaa.entity.OrderChargeItem;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gy.mapper.OrderBackFeeMapper;
import com.gy.mapper.OrderBackfeeltemMapper;
import com.gy.mapper.OrderChargeItemMapper;
import com.gy.mapper.OrderChargeMapper;
import com.gy.service.CareHistoryServiceDto;
import com.gy.vo.OrderChargeDtoListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * @Author GaoYa
 * @Date 2023/3/31 17:21
 * @Version 1.5
 */
@Service
public class CareHistoryServiceDtoImpl implements CareHistoryServiceDto {

    @Autowired(required = false)
    private OrderBackFeeMapper buyerFeeDao;

    @Autowired(required = false)
    private OrderBackfeeltemMapper orderItemDao;

    @Autowired(required = false)
    private OrderChargeItemMapper orderChargeItemDao;

    @Resource
    private OrderChargeMapper orderChargeDao;

    /**
     * 现金收费
     *
     * @param orderChargeDtoListVo
     * @return
     */
    @Override
    public Result XJOrder(OrderChargeDtoListVo orderChargeDtoListVo) {
        //获取患者姓名
        String patientName = orderChargeDtoListVo.getOrderChargeDto().getPatientName();
        //获取挂号单id
        String regId = orderChargeDtoListVo.getOrderChargeDto().getRegId();
        //获取总金额
        BigDecimal orderAmount = orderChargeDtoListVo.getOrderChargeDto().getOrderAmount();
        //创建时间
        LocalDateTime now = LocalDateTime.now();
        //生成orderId
        String orderId = "ODC";
        Random random = new Random();
        for (int i = 0; i < 19; i++) {
            orderId += String.valueOf(random.nextInt(10));
        }
        orderChargeDao.insertAll(regId, orderId, patientName, orderAmount, now);
        List<OrderChargeItem> orderChargeItem = orderChargeDtoListVo.getOrderChargeItemDtoList();
        for (int i = 0; i < orderChargeItem.size(); i++) {
            orderChargeItemDao.updateStatus(orderChargeItem.get(i).getItemId());
        }
        for (int i = 0; i < orderChargeItem.size(); i++) {
            OrderChargeItem orderChargeItem1 = new OrderChargeItem();
            orderChargeItem1.setOrderId(orderId);
            orderChargeItem1.setCoId(orderId);
            orderChargeItem1.setItemId(orderChargeItem.get(i).getItemId());
            orderChargeItem1.setItemType(orderChargeItem.get(i).getItemType());
            orderChargeItem1.setItemName(orderChargeItem.get(i).getItemName());
            orderChargeItem1.setItemNum(orderChargeItem.get(i).getItemNum());
            orderChargeItem1.setItemPrice(orderChargeItem.get(i).getItemPrice());
            orderChargeItem1.setItemAmount(orderChargeItem.get(i).getItemAmount());
            orderChargeItem1.setStatus("1");
            orderChargeItemDao.insert(orderChargeItem1);
        }
        return new Result(2000, "操作成功");
    }

    /**
     * 现金退费
     *
     * @param orderChargeDtoListVo
     * @return
     */
    @Override
    public Result XJBackFee(OrderChargeDtoListVo orderChargeDtoListVo) {
        String chId = orderChargeDtoListVo.getOrderChargeDto().getChId();
        BigDecimal orderAmount = orderChargeDtoListVo.getOrderChargeDto().getOrderAmount();
        String patientName = orderChargeDtoListVo.getOrderChargeDto().getPatientName();
        String regId = orderChargeDtoListVo.getOrderChargeDto().getRegId();
        QueryWrapper<OrderCharge> queryWrap = new QueryWrapper<>();
        queryWrap.eq("reg_id", regId);
        List<OrderCharge> orderBackfees = orderChargeDao.selectList(queryWrap);
        for (OrderCharge orderBackfee : orderBackfees) {
            if (orderBackfee.getPayType().equals("0")) {
                LocalDateTime createTime = LocalDateTime.now();
                String backId = "ODB";
                Random random = new Random();
                for (int i = 0; i < 19; i++) {
                    backId += String.valueOf(random.nextInt(10));
                }
                buyerFeeDao.insertAll(backId, orderAmount, chId, patientName, regId, createTime);
                List<OrderChargeItem> orderChargeItem = orderChargeDtoListVo.getOrderChargeItemDtoList();
                for (int i = 0; i < orderChargeItem.size(); i++) {
                    orderChargeItemDao.updateStatusImt(orderChargeItem.get(i).getItemId());
                }
                for (int i = 0; i < orderChargeItem.size(); i++) {
                    OrderBackfeeItem orderBackfeeItem = new OrderBackfeeItem();
                    orderBackfeeItem.setBackId(backId);
                    orderBackfeeItem.setCoId(backId);
                    orderBackfeeItem.setItemId(orderChargeItem.get(i).getItemId());
                    orderBackfeeItem.setItemNum(orderChargeItem.get(i).getItemNum());
                    orderBackfeeItem.setItemName(orderChargeItem.get(i).getItemName());
                    orderBackfeeItem.setItemPrice(orderChargeItem.get(i).getItemPrice());
                    orderBackfeeItem.setItemType(orderChargeItem.get(i).getItemType());
                    orderBackfeeItem.setItemAmount(orderChargeItem.get(i).getItemAmount());
                    orderBackfeeItem.setStatus("2");
                    orderItemDao.insert(orderBackfeeItem);
                }
                return new Result(2000, "现金退费成功");
            }
            return new Result(5000, "改退费不支持微信退费");
        }
        return null;
    }
}
